package UCM;

import Model.StartServer;
import Observer.Observer;
import Observer.Subject;
import View.ServerGUI;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Observable;

/**
 *
 * @author Jason Sorbello
 */
/**
 * Precondition: The user clicks the start server button.

1. System checks there is no server instance already running
2. System creates new server socket
3. System creates new thread to accept connections
4. System notifies client of connection

Postcondition: Server is running.

Unsuccessful Outcomes:

1a) There is already a server instance running
System updates the UI informing the user there is already a server iinstance running.
 */
public class UCStartServer extends Observable implements UCController {

    private StartServer m_model = new StartServer();
    private ServerGUI m_view;// = new ServerGUI();
    private ArrayList observers = new ArrayList();

    public UCStartServer() {
        //throw new UnsupportedOperationException("Not yet implemented");
    }

    public UCStartServer(ServerGUI aThis) {
        //throw new UnsupportedOperationException("Not yet implemented");
    }

    public UCStartServer(StartServer model, ServerGUI view) {
        //this.view = view;
        //view.loadTo(model);
        m_model = model;
        m_view = view;
    }

    public void run() {
        // 1. System checks there is no server instance already running
        if (!m_model.checkRunning()) {
            // 2. System creates new server socket
            m_model.newServerSocket();
            // 3. System creates new thread to accept connections
            m_model.newThread();
            // 4. System notifies client of connection
            notifyObservers(true);
        }
    }

    public void addObserver(Observer o) {
        observers.add(o);
    }

    public void removeObserver(Observer o) {
        observers.remove(o);
    }
}
